package in.muton.edushare.validators.formvalidators;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;

import com.edushare.common.vo.Users;

public class UserPasswordValidator extends AbstractFormValidator {
	private static final long serialVersionUID = -3372004636411242231L;

	private Users user;
	private PasswordTextField originalPTF;
	public UserPasswordValidator(Users user,PasswordTextField originalPTF){
		this.user = user;
		this.originalPTF = originalPTF;
	}
	
	@Override
	protected String resourceKey(){
		return "UserPasswordValidator";
	}
	
	public FormComponent<?>[] getDependentFormComponents() {
		return null;
	}

	public void validate(Form<?> arg0) {
		String pwd = originalPTF.getInput();
		pwd = DigestUtils.sha256Hex(pwd);
		if(pwd.compareTo(user.getPassword()) != 0 ){
			error(originalPTF,"Incorrect Password");
		}
	}

}
